/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-02-8       金鹏       the first version
 */

#include "cmn_uart.h"

/* 串口设备表 ： 0-5 -> uart1-uart6*/
struct CMN_UART uart_device[6] = {
/* 串口设备句柄,    串口接收信号量,   串口中断接收函数指针                                                                                         */
    {RT_NULL,       RT_NULL,        uart1_input},                       /* uart1*/
    {RT_NULL,       RT_NULL,        uart2_input},                       /* uart2*/
    {RT_NULL,       RT_NULL,        uart3_input},                       /* uart3*/
    {RT_NULL,       RT_NULL,        uart4_input},                       /* uart4*/
    {RT_NULL,       RT_NULL,        uart5_input},                       /* uart5*/
    {RT_NULL,       RT_NULL,        uart6_input},                       /* uart6*/
};

static rt_err_t uart1_input(rt_device_t dev, rt_size_t size)
{
    /* 串口接收到数据后产生中断，调用此回调函数，然后发送接收信号量 */
    rt_sem_release(uart_device[0].uart_rx_sem);

    return RT_EOK;
}

static rt_err_t uart2_input(rt_device_t dev, rt_size_t size)
{
    /* 串口接收到数据后产生中断，调用此回调函数，然后发送接收信号量 */
    rt_sem_release(uart_device[1].uart_rx_sem);

    return RT_EOK;
}

static rt_err_t uart3_input(rt_device_t dev, rt_size_t size)
{
    /* 串口接收到数据后产生中断，调用此回调函数，然后发送接收信号量 */
    rt_sem_release(uart_device[2].uart_rx_sem);

    return RT_EOK;
}

static rt_err_t uart4_input(rt_device_t dev, rt_size_t size)
{
    /* 串口接收到数据后产生中断，调用此回调函数，然后发送接收信号量 */
    rt_sem_release(uart_device[3].uart_rx_sem);

    return RT_EOK;
}

static rt_err_t uart5_input(rt_device_t dev, rt_size_t size)
{
    /* 串口接收到数据后产生中断，调用此回调函数，然后发送接收信号量 */
    rt_sem_release(uart_device[4].uart_rx_sem);

    return RT_EOK;
}

static rt_err_t uart6_input(rt_device_t dev, rt_size_t size)
{
    /* 串口接收到数据后产生中断，调用此回调函数，然后发送接收信号量 */
    rt_sem_release(uart_device[5].uart_rx_sem);

    return RT_EOK;
}

static void BAUD_Change(rt_device_t *device , int baudrate){
    struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;

    config.baud_rate = baudrate;

    rt_device_control(*device, RT_DEVICE_CTRL_CONFIG, &config);
}

/*
* CMN_Uart_Init
* @Param name
*
* Change Logs:
* Date           Author       Notes
* 2025-2-8        金鹏                共通串口初始化
*/
ENTRY CMN_Uart_Init(char *name)
{
    /* 获取当前串口号*/
    int select = *(name+4)-'0' -1;

    /* 已经初始化*/
    if(uart_device[select].uart_serial != RT_NULL){
        return RT_NG;
    }

    /* 寻找uart设备*/
    if(! (uart_device[select].uart_serial = rt_device_find(name))){
        rt_kprintf("find %s failed!\n", name);
        return -RT_ERROR;
    }

    /* 串口2需要更改波特率为38400*/
    if(select == 1){
        BAUD_Change(&(uart_device[select].uart_serial),BAUD_RATE_38400);
    }

    /* 串口4需要更改波特率为9600*/
    if(select == 3){
        BAUD_Change(&(uart_device[select].uart_serial),BAUD_RATE_9600);
    }

    /* 串口6需要更改波特率为115200*/
    if(select == 5){
        BAUD_Change(&(uart_device[select].uart_serial),BAUD_RATE_115200);
    }

    /* 初始化信号量 */
    if((uart_device[select].uart_rx_sem = rt_sem_create(name, 0, RT_IPC_FLAG_FIFO)) == RT_NULL){
       rt_kprintf("%s sem create failed\n",name);
    }

    /* 以中断接收及轮询发送模式打开串口设备*/
    rt_device_open(uart_device[select].uart_serial, RT_DEVICE_FLAG_INT_RX);

    /* 设置接收回调函数 */
    rt_device_set_rx_indicate(uart_device[select].uart_serial, uart_device[select].input);

    return RT_EOK;
}
